import FrameworkManager from "./frameworkManager"
import NodePool from "./NodePool"
import FlyPropellerPrefabCtrl from "./FlyPropellerPrefabCtrl"
import MatchGameConfig from "./MatchGameConfig"

const { ccclass, property } = cc._decorator;

@ccclass
class FlyPropellerCtrl {
    flyItem = null;
    flyItemParent = null;
    flyPool = new cc.NodePool("FlyPropeller");
    isReady = false;
    propplerFlyCount = 0;

    static _instance: FlyPropellerCtrl;
    static getInstance() {
        if (!this._instance) {
            this._instance = new FlyPropellerCtrl();
        }
        return this._instance;
    }

    ready(e, t) {
        this.flyItem = e;
        this.flyItemParent = t;
        NodePool.initPool(this.flyItem, 0, MatchGameConfig.SquarePool.FlyPropeller);
        this.isReady = true;
        this.propplerFlyCount = 0;
    }

    getFlyPropplerCount() {
        return this.propplerFlyCount;
    }

    init() { }

    createFlyPropellerItem(e, t) {
        const a = this.flyPool.size() > 0 ? this.flyPool.get() : cc.instantiate(this.flyItem);
        this.flyItemParent.addChild(a);
        a.active = true;
        if (!a.getComponent(FlyPropellerPrefabCtrl)) {
            a.addComponent(FlyPropellerPrefabCtrl);
        }
        const o = a.getComponent(FlyPropellerPrefabCtrl);
        o.init(e, t);
        this.propplerFlyCount += 1;
        return o;
    }

    destoryFlyItem(e) {
        FrameworkManager.log("回收小飞机", e);
        this.flyPool.put(e);
        this.propplerFlyCount -= 1;
    }
}

export default FlyPropellerCtrl.getInstance();